home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / database / cdbms / qd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-06-01  |  5.1 KB  |  240 lines

  1. /* -------------------------- qd.c ----------------------- */
  2. /* A query program.  Enter the name of the data base file and
  3.  * (optionally) a list of data elements.  A screen is built
  4.  * and the file is updated based upon the data entered.
  5.  */
  6. #include <stdio.h>
  7. #include "keys.h"
  8. #include "cdata.h"
  9.  
  10. static int file;
  11. int existing_record;
  12. char *rb;                /* record buffer */
  13. char *hb;                /* hold buffer */
  14. char *sc;                /* screen buffer   */
  15. int len;                /* record length   */
  16. char *malloc();
  17. int fl[] = {0, -1};
  18. int iplist [MXELE+1];
  19. int *els;
  20. void query(), rcdin(), rcdout(), set_trap(), clear_record();
  21. int key_entry();
  22.  
  23. main(argc, argv)
  24. int argc;
  25. char *argv[];
  26. {
  27.     if (argc > 1)    {
  28.         if ((file = filename(argv[1])) != ERROR)    {
  29.             if (argc == 2)    {
  30.                 len = rlen(file);
  31.                 els = file_ele [file];
  32.             }
  33.             else if (ellist(argc-2, argv+2, iplist) == OK)    {
  34.                 len = epos(0, iplist);
  35.                 els = iplist;
  36.             }
  37.             else
  38.                 exit(1);
  39.             sc = malloc(len);            /* screen buffer */
  40.             rb = malloc(rlen(file));    /* record buffer */
  41.             hb = malloc(rlen(file));    /* hold buffer   */
  42.             if (sc == (char *) 0 ||
  43.                     rb == (char *) 0 ||
  44.                         hb == (char *) 0)    {
  45.                 printf("\nOut of memory");
  46.                 exit(1);
  47.             }
  48.             init_rcd(file, rb);
  49.             init_rcd(file, hb);
  50.             init_screen(argv[1], els, sc);
  51.             query();
  52.             free(hb);
  53.             free(rb);
  54.             free(sc);
  55.         }
  56.     }
  57. }
  58.  
  59.  
  60.  
  61. /* -------------- process the query ------------ */
  62. static void query()
  63. {
  64.     int term = 0;
  65.     
  66.     *fl = file;
  67.     db_open("", fl);
  68.     clrrcd(sc, els);
  69.     set_trap();
  70.     while (term != ESC)    {
  71.         term = data_entry();
  72.         switch (term)    {
  73.             /* ----------- GO --------------- */
  74.             case F1:    rcdout();
  75.                         break;
  76.             /* ------------- First record ---------- */
  77.             case HOME:    rcdout();
  78.                         if (first_rcd(file, 1, rb) == ERROR)
  79.                             post_notice("Empty file");
  80.                         else
  81.                             rcdin();
  82.                         break;
  83.             /* ------------- First record ---------- */
  84.             case END:    rcdout();
  85.                         if (last_rcd(file, 1, rb) == ERROR)
  86.                             post_notice("Empty file");
  87.                         else
  88.                             rcdin();
  89.                         break;
  90.             /* ------------- Previous record -------- */
  91.             case PGUP:    rcdout();
  92.                         if (prev_rcd(file, 1, rb) == ERROR)    {
  93.                             post_notice("Beginning of file");
  94.                             if (first_rcd(file, 1, rb) ==
  95.                                                     ERROR)    {
  96.                                 post_notice("Empty file");
  97.                                 break;
  98.                             }
  99.                         }
  100.                         rcdin();
  101.                         break;
  102.             /* ------------- Next record ------------- */
  103.             case PGDN:    rcdout();
  104.                         if (next_rcd(file, 1, rb) == ERROR)    {
  105.                             post_notice("At end of file");
  106.                             if (last_rcd(file, 1, rb) ==
  107.                                                     ERROR)    {
  108.                                 post_notice("Empty file");
  109.                                 break;
  110.                             }
  111.                         }
  112.                         rcdin();
  113.                         break;
  114.             /* -------------- Delete record ------------- */
  115.             case F7:    if (spaces(rb)== 0)    {
  116.                             post_notice("Verify w/F7");
  117.                             if (get_char() == F7)    {
  118.                                 del_rcd(file);
  119.                                 clear_record();
  120.                             }
  121.                             clear_notice();
  122.                         }
  123.                         break;
  124.             case ESC:    if (spaces(sc))
  125.                             break;
  126.                         clear_record();
  127.                         term = 0;
  128.                         break;
  129.             default:    break;
  130.         }
  131.     }
  132.     clear_screen();
  133.     db_cls();
  134. }
  135.  
  136. /* ----------- clear out the record area -------------- */
  137. static void clear_record()
  138. {
  139.     int i = 0;
  140.     while (index_ele [file] [0] [i])
  141.         protect(index_ele[file][0][i++],FALSE);
  142.     clrrcd(sc, els);
  143.     existing_record = FALSE;
  144. }
  145.  
  146. /* ----------- get the data base file record ------------- */
  147. static void rcdin()
  148. {
  149.     int i = 0;
  150.  
  151.     if (empty(rb, rlen(file)) == 0)    {
  152.         rcd_fill(rb, sc, file_ele [file], els);
  153.         mov_mem(rb, hb, rlen(file));
  154.         existing_record = TRUE;
  155.         while (index_ele [file] [0] [i])
  156.             protect(index_ele[file][0][i++],TRUE);
  157.     }
  158. }
  159.  
  160.  
  161. /* ------- add or update the data base file record ------ */
  162. static void rcdout()
  163. {
  164.     if (empty(sc, len) == 0)    {
  165.         rcd_fill(sc, rb, els, file_ele[file]);
  166.         if (existing_record)    {
  167.             if (same() == 0)    {
  168.                 post_notice("Returning record");
  169.                 rtn_rcd(file, rb);
  170.             }
  171.         }
  172.         else    {
  173.             post_notice("New record added");
  174.             if (add_rcd(file, rb) == ERROR)
  175.                 dberror();
  176.         }
  177.         clear_record();
  178.     }
  179. }
  180.  
  181. /* -------- test for an empty record buffer ---------- */
  182. static int empty(b, l)
  183. char *b;
  184. int l;
  185. {
  186.     while (l--)
  187.         if (*b && *b != ' ')
  188.             return FALSE;
  189.         else
  190.              b++;
  191.     return TRUE;
  192. }
  193.  
  194. /* ---------- test two record buffers for equality ------- */
  195. static int same()
  196. {
  197.     int ln = rlen(file);
  198.  
  199.     while (--ln)
  200.         if (*(rb + ln) != *(hb + ln))
  201.             break;
  202.     return (*(rb + ln) == *(hb + ln));
  203. }
  204.  
  205. /* ------ set the query screen's key element trap --------- */
  206. static void set_trap()
  207. {
  208.     int i = 0;
  209.  
  210.     while (index_ele [file] [0] [i])
  211.         i++;
  212.     edit(index_ele [file] [0] [i-1], key_entry);
  213. }
  214.  
  215. /* --- come here when the primary key has been entered ---- */
  216. static int key_entry(s)
  217. char *s;
  218. {
  219.     char key [MXKEYLEN];
  220.     int i;
  221.  
  222.     if (spaces(s))
  223.         return OK;
  224.     *key = '\0';
  225.     i = 0;
  226.     while (index_ele [file] [0] [i])    {
  227.         protect(index_ele[file][0][i],TRUE);
  228.         strcat(key, sc + epos(index_ele[file][0][i++], els));
  229.     }
  230.     if (find_rcd(file, 1, key, rb) == ERROR)    {
  231.         post_notice("New record");
  232.         existing_record = FALSE;
  233.         return OK;
  234.     }
  235.     rcdin();
  236.     tally();
  237.     return OK;
  238. }
  239.  
  240.